package com.itheima.utils;

import com.alibaba.druid.pool.DruidDataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.SQLException;

@Component
public  class Myutils {

    private ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();


    @Autowired
    private DruidDataSource druidDataSource;

    public  Connection getConnection() throws SQLException {
        Connection connection = threadLocal.get();

        if (connection == null) {
            connection  = druidDataSource.getConnection();
            threadLocal.set(connection);
        }

        return connection;


    }
}